import { Injectable, CanActivate, ExecutionContext } from "@nestjs/common";
import { AuthService } from "./auth.service";
import { Request } from 'express';
import { MsgService } from "src/msg/msg.service";

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(
    private readonly authService: AuthService,
    private readonly MSG: MsgService,
  ) {}

  canActivate(context: ExecutionContext) {
    const request = context.switchToHttp().getRequest<Request>();

    // 获取token
    const authorizeation = request.header('authorization');

    if (!authorizeation) {
      // return false;
      this.MSG.fail('未登录', 403);
    }

    return true;
  }
}